/**
 * DoubleEditor.java
 * Created at 2016年10月22日
 * Created by Jackie Liu
 * Copyright (C) 2016 Jackie Liu, All rights reserved.
 */
package com.huabo.editors;

import com.huabo.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;

import java.beans.PropertyEditorSupport;


/**
 * <p>ClassName: DoubleEditor</p>
 * <p>Description: Double类型属性编辑器</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年10月22日</p>
 */
public class DoubleEditor extends PropertyEditorSupport {

    /* (non-Javadoc)
     * <p>Title: setValue</p>
     * <p>Description: </p>
     * @param value
     * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
     */
    @Override
    public void setValue(Object value) {
        if (value instanceof Double) {
            super.setValue((Double) value);
        } else {
            super.setValue(value);
        }
    }

    /* (non-Javadoc)
     * <p>Title: setAsText</p>
     * <p>Description: </p>
     * @param text
     * @throws IllegalArgumentException
     * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isEmpty(text)
                || !ValidateUtils.validateNumber(text)) {
            this.setValue(0);
            return;
        }
        this.setValue(Double.valueOf(text));
    }
}
